/* 
   This program imports the concrete imputation .csv files into SAS,
   merges the survu_id and firmid, and saves the merged files.

   input files:
	concrete_predicted02.csv
	concrete_predicted07.csv

   output files:
	concrete.concrete_predicted02
	concrete.concrete_predicted07
*/

%include "ASMimplibs.sas";


/*** BEGIN MACROS ***/

%MACRO import_predicted(year=);


            data WORK.CONCRETE_predicted&year                      ;
            %let _EFIERR_ = 0; /* set the ERROR detection macro variable */
            infile "concrete_predicted&year..csv" delimiter = ',' MISSOVER DSD lrecl=32767 firstobs=2 ;
               informat number $5. ;
               informat PH best32. ;
               informat CM best32. ;
               informat PW best32. ;
               informat SW best32. ;
               informat WW best32. ;
               informat TVS best32. ;
               informat BEA_CEA_Code $5. ;
               informat CEA_ddensity best32. ;
               informat nonpw best32. ;
               informat tab best32. ;
               informat energy best32. ;
               informat impsetnum best32. ;

               format number $5. ;
               format PH best12. ;
               format CM best12. ;
               format PW best12. ;
               format SW best12. ;
               format WW best12. ;
               format TVS best12. ;
               format BEA_CEA_Code $5. ;
               format CEA_ddensity best12. ;
               format nonpw best12. ;
               format tab best12. ;
               format energy best12. ;
               format impsetnum best12. ;

            input
                        number $
                        PH 
                        CM
                        PW
                        SW
                        WW
                        TVS
                        BEA_CEA_Code $
                        CEA_ddensity 
                        nonpw
                        tab
                        energy
                        impsetnum
            ;
            if _ERROR_ then call symputx('_EFIERR_',1);  /* set ERROR detection macro variable */
            run;



data concrete_predicted&year (drop = NUMBER);
 set concrete_predicted&year;
 NUMBER_NUM = input(NUMBER,5.);
run;

PROC DATASETS  LIBRARY=WORK;
 MODIFY concrete_predicted&year;
 rename NUMBER_NUM = NUMBER;
RUN;

PROC SORT DATA=concrete_predicted&year;
 by NUMBER;
RUN;

/** Merge survu_id **/

data concrete&year._ids (keep = NUMBER survu_id firmid wt);
 set concrete.concrete&year._gooddata_ids;
 NUMBER = _N_;
run;

data concrete_predicted&year rejimp&year rejid&year ;
 merge concrete&year._ids (in=inid) concrete_predicted&year (in=inimp); 
by NUMBER;
if inid and inimp then output concrete_predicted&year;
else if inid then output rejimp&year;
else output rejid&year;
run;

data concrete.concrete_predicted&year (drop = impsetnum);
 set concrete_predicted&year ;
 _IMPUTE_= impsetnum ;
run;

%MEND;



/**************************/
/******  BEGIN PROGRAM ****/
/**************************/

%import_predicted(year=02);
%import_predicted(year=07);




